<?php

namespace App\Http\Controllers\App;

use App\Http\Resources\App\CareerDetailResource;
use App\Http\Resources\App\CareerIndexResource;
use App\Models\Career;
use App\Models\User;
use App\Models\UserCareer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use phpDocumentor\Reflection\DocBlock\Tags\Throws;

class CareerController extends Controller
{
    //职业列表-选择职业方向
    public function index(Request $request, Career $career)
    {
        return CareerIndexResource::collection($career->with(['users' => function ($query) use ($request) {
            return $query->where('user_id', $request->user()->id);
        }])->get());
    }

    //职业详情，可以查看技能信息
    public function detail(Request $request, Career $career)
    {
        return new CareerDetailResource($career->load(['skills', 'skills.users' => function ($query) use ($request) {
            return $query->where('user_id', $request->user()->id)->withPivot(['learn_count']);
        }]));
    }


    //选择职业的操作
    public function select(Request $request, Career $career)
    {
        UserCareer::where('user_id', $request->user()->id)->update(['is_selected' => 0]);
        return $career->users()->sync($request->user()->id);
    }

//当前职业
    public function current(Request $request)
    {
        $career = $request->user()->careers()->wherePivot('is_selected', 1)->with(['skills'])->first();
        if ($career) {
            return new CareerDetailResource($career);
        } else {
            abort(404, '先选择职业');
        }

    }

//我之前选择的职业
    public function previous(Request $request)
    {
        return $request->user()->careers;
    }

//遗忘职业
    public function forget(Request $request, Career $career)
    {
        $career->users()->detach($request->user()->id);
    }

}
